﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Settings;
using Utils;

namespace PanasonicWarranty.Business
{
    public partial class OperatorPrefixCode
    {
        #region Singleton Pattern
        private static object _lock = new object();
        private static List<OperatorPrefixCode> _instance;
        public static List<OperatorPrefixCode> GetInstance()
        {
            lock (_lock)
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        using (var context = new DBSmsDataContext(AppSetting.ConnectionString))
                        {
                            _instance = context.OperatorPrefixCodes.ToList();
                        }
                    }
                }
            }
            return _instance;
        }

        private static Dictionary<string, int> _dict;
        public static Dictionary<string, int> GetDictionaryPrefixCode()
        {
            if (_dict == null)
            {
                _dict = new Dictionary<string, int>();
                foreach (OperatorPrefixCode item in GetInstance())
                {
                    _dict.Add(item.prefix_code, item.operator_id ?? -1);
                }
            }
            return _dict;
        }
        public static void ReloadData()
        {
            lock (_lock)
            {
                using (var context = new DBSmsDataContext(AppSetting.ConnectionString))
                {
                    _instance = context.OperatorPrefixCodes.ToList();
                    if (_dict == null)
                        _dict = new Dictionary<string, int>();
                    else
                        _dict.Clear();
                    foreach (OperatorPrefixCode item in _instance)
                    {
                        _dict.Add(item.prefix_code, item.operator_id ?? -1);
                    }
                }
            }
        }
        #endregion Singleton Pattern
    
        public static int GetOperatorID(string phonenumber)
        {
            try
            {
                var source = GetDictionaryPrefixCode();
                for (int i = 4; i <= 7; i++)
                {
                    string prefix = phonenumber.Substring(0, i);
                    if (source.ContainsKey(prefix))
                        return source[prefix];
                }
                return -1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //public static int GetOperatorID(string phonenumber, Dictionary<string, int> source)
        //{
        //    try
        //    {
        //        for (int i = 4; i <= 7; i++)
        //        {
        //            string prefix = phonenumber.Substring(0,i);
        //            if (source.ContainsKey(prefix))
        //                return source[prefix];
        //        }
        //        return -1;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

        public static bool IsMobilePhone(string phone, Dictionary<string, int> source)
        {
            try
            {
                return IsMobilePhone(GetOperatorID(phone));
            }
            catch
            {
                return false;
            }
        }

        public static bool IsMobilePhone(int operator_id)
        {
            if (operator_id != 9)
                return true;
            return 
                false;
        }

        public static bool ValidatePhoneNumber(string phonenumber)
        {
            var phone = phonenumber.ToNormalPhoneNumber();
            return GetInstance().Any(p => phone.IndexOf(p.prefix_code) == 0
                            && phone.Length >= p.min_length
                            && phone.Length <= p.max_length);
        }

        public static bool ValidatePhoneNumber(ref string phonenumber)
        {
            var phone = phonenumber.ToNormalPhoneNumber();
            var retvalue = GetInstance().Any(p => phone.IndexOf(p.prefix_code) == 0
                            && phone.Length >= p.min_length
                            && phone.Length <= p.max_length);
            phonenumber = phone;
            return retvalue;
        }

        public static bool ValidatePhoneNumber(string[] phonenumber)
        {
            foreach (string item in phonenumber)
            {
                if (!ValidatePhoneNumber(item))
                    return false;
            }
            return true;
        }

        public static bool ValidatePhoneNumber(IEnumerable<string> phonenumber)
        {
            foreach (string item in phonenumber)
            {
                if (!ValidatePhoneNumber(item))
                    return false;
            }
            return true;
        }
    }
}
